package worklog.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import worklog.db.EntryDB;
import worklog.dto.DayDTO;
import worklog.dto.ResponseDTO;

import worklog.DaysCalculator;

/**
 * Handles Project-Log requests
 */

@WebServlet("/projectWeek")
public class ProjectWeek extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private EntryDB entryDB;
	private ResponseDTO responder = new ResponseDTO();

    public ProjectWeek() throws Exception{ super(); }
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		//doGet(request, response); 
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		entryDB = new EntryDB();
		PrintWriter out = response.getWriter();
		response.setContentType("application/json");
    	if (entryDB.isReady()) out.print(processRequest(request));
    	else out.print(responder.showError("DB connection error"));
    	entryDB.close();
    	out.close();
	}
	
	
	private String processRequest(HttpServletRequest request){
    	String fromDateStr = request.getParameter("from");
    	String toDateStr = request.getParameter("to");
    	String projectIdStr = request.getParameter("projectId");
    	String[] userIdsArr = request.getParameterValues("userId");
    	
    	if (fromDateStr == null || fromDateStr.isEmpty()) return responder.showError("Incorrect parameters");
    	if (toDateStr == null || toDateStr.isEmpty()) return responder.showError("Incorrect parameters");

    	// convert projectId to int
    	int projectId = -1;
    	if (!projectIdStr.isEmpty()) projectId = Integer.parseInt(projectIdStr);
    	
    	// convert userIds to ints
    	int[] userIds = null;
    	try{
	    	if (userIdsArr != null && userIdsArr.length > 0){
	    		if (userIdsArr.length == 1 && userIdsArr[0] == "") throw new Exception("no userIDs");
	    		userIds = new int[userIdsArr.length];
	    		for (int i=0, il = userIdsArr.length; i < il; i++){
	    			if (!userIdsArr[i].isEmpty()) userIds[i] = Integer.parseInt(userIdsArr[i]);
	    		}
	    	}
    	}
    	catch(Exception e){}
    	
    	ArrayList<String> week = new ArrayList<String>();
    	DayDTO day = new DayDTO();
    	String[] weekDays = DaysCalculator.getDays(fromDateStr, toDateStr);
    	
    	if (weekDays.length > 100) return responder.showError("Date range is too big. Try smaller range.");
    	
    	for (String dat : weekDays){
    		try { day = entryDB.getProjectDay(dat, projectId, userIds); } catch (SQLException e) {}
    		if (day != null) week.add(day.toString());
    	}
		
		if (week.size()>0) return week.toString();
		return responder.showError("No entries found");
	}
}